Everything about Breadth-first Search totally explained
In graph theory, breadth-first search (BFS) is a graph search algorithm that begins at the root node and explores all the neighboring nodes. Then for each of those nearest nodes, it explores their unexplored neighbor nodes, and so on, until it finds the goal.
How it works
BFS is an uninformed search method that aims to expand and examine all nodes of a graph systematically in search of a solution. In other words, it exhaustively searches the entire graph without considering the goal until it finds it. It doesn't use a heuristic.
From the standpoint of the algorithm, all child nodes obtained by expanding a node are added to a FIFOqueue. In typical implementations, nodes that have not yet been examined for their neighbors are placed in some container (such as a queue or linked list) called "open" and then once examined are placed in the container "closed".
Algorithm (informal)
Put the root node on the queue.
Pull a node from the beginning of the queue and examine it.
If the searched element is found in this node, quit the search and return a result.
Otherwise push all the (so-far-unexamined) successors (the direct child nodes) of this node into the end of the queue, if there are any.
If the queue is empty, every node on the graph has been examined -- quit the search and return "not found".
Repeat from Step 2.
Note: Using a stack instead of a queue to store the nodes yet to be visited would turn this algorithm into a depth-first search.
C implementation
Algorithm of Breadth-first search:
void BFS(VLink G[], int v)
» it also stores the parents of each node, from which you can get the path.
Features
Space Complexity
Since all of the nodes of a level must be saved until their child nodes in the next level have been generated, the space complexity is proportional to the number of nodes at the deepest level. Given a branching factor and graph depth the asymptotic space complexity is the number of nodes at the deepest level, . When the number of vertices and edges in the graph are known ahead of time, the space complexity can also be expressed as where is the cardinality of the set of edges (the number of edges), and is the cardinality of the set of vertices. In the worst case the graph has a depth of 1 and all vertices must be stored. Since it's exponential in the depth of the graph, breadth-first search is often impractical for large problems on systems with bounded space.
Time Complexity
Since in the worst case breadth-first search has to consider all paths to all possible nodes the time complexity of breadth-first search is which asymptotically approaches . The time complexity can also be expressed as since every vertex and every edge will be explored in the worst case.
Completeness
Breadth-first search is complete. This means that if there's a solution breadth-first search will find it regardless of the kind of graph. However, if the graph is infinite and there's no solution breadth-first search will diverge.
Optimality
For unit-step cost, breadth-first search is optimal. In general breadth-first search isn't optimal since it always returns the result with the fewest edges between the start node and the goal node. If the graph is a weighted graph, and therefore has costs associated with each step, a goal next to the start doesn't have to be the cheapest goal available. This problem is solved by improving breadth-first search to uniform-cost search which considers the path costs. Nevertheless, if the graph isn't weighted, and therefore all step costs are equal, breadth-first search will find the nearest and the best solution.
Applications of BFS
Breadth-first search can be used to solve many problems in graph theory, for example:
The set of nodes reached by a BFS (breadth-first search) are the largest connected component containing the start node.
Testing bipartiteness
BFS can be used to test bipartiteness, by starting the search at any vertex and giving alternating labels to the vertices visited during the search. That is, give label 0 to the starting vertex, 1 to all its neighbours, 0 to those neighbours' neighbours, and so on. If at any step a vertex has (visited) neighbours with the same label as itself, then the graph isn't bipartite. If the search ends without such a situation occurring, then the graph is bipartite.
Do you know how hard it is to get a link from a large encyclopaedia? Well we're different and will prove it. To get a link from us just add the following HTML to your site on a relevant page:
Then simply click through this link from your web page. Our crawlers will verify your link, extract the title of your web page and instantly add a link back to it. If you like you can remove the words Totally Explained and embed the link in article text.
As long as your link remains in place, we'll keep our link to you right here. Please play fair - our crawlers are watching. Your site must be closely related to this one's topic. Any kind of spamming, dubious practises or removing the link will result in your link from us being dropped and, potentially, your whole site being banned.